
Ext.ux.ComboBoxCheckTree = function() {
	this.treeId = Ext.id() + '-tree';
	this.maxHeight = arguments[0].maxHeight || arguments[0].height
			|| this.maxHeight;
	this.tpl = new Ext.Template('<tpl for="."><div style="height:'
			+ this.maxHeight + 'px"><div id="' + this.treeId
			+ '"></div></div></tpl>');
	this.store = new Ext.data.SimpleStore({
		fields : [],
		data : [[]]
	});
	this.selectedClass = '';
	this.mode = 'local';
	this.triggerAction = 'all';
	this.onSelect = Ext.emptyFn;
	this.editable = false;
	this.selectValueModel = 'leaf';
	
	//只能选择同一层次下的
	this.firstCheckNode=null;
	this.onlyCanSelectSameTeam=false;
	var TS=this;
	var search=function(){
		var value=this.getValue();
		var find=null;
		var search_=function(node){
			if(find!=null)return;
			find=node.findChild('text',value);
			if(node.hasChildNodes()&&find==null){
				for(var i=0;i<node.childNodes.length;i++){
					search_(node.childNodes[i]);
				}
			}
		}
		search_(TS.tree.root);
		if(find!=null)find.select();
	}
	//tree default config
	this.treeDefaultConfig={
		xtype:'treepanel',
		checkModel: 'cascade',   //cascade selection
		onlyLeafCheckable: false,//all nodes with checkboxes
		animate: true,
		rootVisible: false,
		autoScroll:true,
		border:false,
		width:this.width,
		height:this.maxHeight,
//		bbar: ['名称：',{xtype:'trigger',id: 'name',width:120,  
//             triggerClass:'x-form-search-trigger',onTriggerClick:search  
//         }],  
   	 	root : new Ext.tree.AsyncTreeNode({id:'0',text:'root'})
	}
	Ext.ux.ComboBoxCheckTree.superclass.constructor.apply(this, arguments);
}

Ext.extend(Ext.ux.ComboBoxCheckTree, Ext.form.ComboBox, {

	checkField : 'checked',
	separator : ',',
	initEvents : function() {
		Ext.ux.ComboBoxCheckTree.superclass.initEvents.apply(this, arguments);
		this.keyNav.tab = false;

	},

	initComponent : function() {
		this.on({
			scope : this
		});

	},
	 reset : function(){
       this.value = this.originalValue;
        if(this.rendered){
            this.el.dom.value = (this.value === null || this.value === undefined ? '' : this.value);
            this.validate();
        }
       this.clearInvalid();
       if(this.tree&&this.tree.rendered){
       	this.clearValue();
       }
    },
	expand : function() {
		var ts=this;
		Ext.ux.ComboBoxCheckTree.superclass.expand.call(this);
		//bluespring 修改当combo没有设置高度时使tree的高度自适应
		if (!this.tree.rendered) {
			if(!this.height&&!this.tree.height){//没有设置高度，自适应
				 this.innerList.setOverflow('hidden');
				 this.restrictHeight();
				this.on('expand',function(){
					var height=ts.innerList.getHeight();
					Ext.get(ts.treeId).parent().setHeight(height);
					ts.tree.setHeight(height);
				})
				
				this.treeDefaultConfig.height=ts.innerList.getHeight();
				Ext.get(ts.treeId).parent().setHeight(ts.innerList.getHeight());
			}
			
			
			Ext.applyIf(this.tree,this.treeDefaultConfig);
			if (this.tree.xtype) {
				this.tree = Ext.ComponentMgr.create(this.tree, this.tree.xtype);
			}
			this.tree.render(this.treeId);
			var combox = this;
			this.tree.on('check', function(node, checked) {
				if(ts.tree.checkModel=='single')ts.collapse();
				combox.validateCheck(node);
				combox.setValue();
			});
			this.tree.on('checkchange',function(node){
				combox.setCkeckEmpty();
			});
			var root = this.tree.getRootNode();
			if (!root.isLoaded())
				root.reload();
		}
	},
	setCkeckEmpty:function(){
		var checkedNodes = this.tree.getChecked();
		if(checkedNodes.length==0)this.firstCheckNode=null
	},
	validateCheck:function(node){
		if(this.onlyCanSelectSameTeam){
			var checkedNodes = this.tree.getChecked();
			if(this.firstCheckNode==null){
			 	this.firstCheckNode=node;
			}
			var validPath=this.firstCheckNode.getPath().substring(0,this.firstCheckNode.getPath().lastIndexOf('/'));
			for (var i = 0; i < checkedNodes.length; i++) {
				var node = checkedNodes[i];
				if(node.getPath().indexOf(validPath)!=0)node.ui.toggleCheck(false);
			}
		}
		
	},
	setValue : function() {
		var values = [];
		var texts = [];
		if(!this.tree)this.v
		var root = this.tree.getRootNode();

		var checkedNodes = this.tree.getChecked();
		for (var i = 0; i < checkedNodes.length; i++) {
			var node = checkedNodes[i];
			if (this.selectValueModel == 'all'
					|| (this.selectValueModel == 'leaf' && node.isLeaf())
					|| (this.selectValueModel == 'folder' && !node.isLeaf())) {
				values.push(node.id);
				texts.push(node.text);
			}
		}

		this.value = values.join(this.separator);
		this.setRawValue(texts.join(this.separator));
		if (this.hiddenField) {
			this.hiddenField.value = this.value;
		}

	},
	
	manualSetValue : function(values, displays){
		this.value = values;
		this.setRawValue(displays);
	},

	getValue : function() {
		return this.value || '';
	},
	
	clearValue : function() {
		this.value = '';
		this.setRawValue(this.value);
		if (this.hiddenField) {
			this.hiddenField.value = '';
		}
		this.tree.selModel.clearSelections();
	}
});

Ext.reg('combochecktree', Ext.ux.ComboBoxCheckTree);